package com.ihunanren.biz.function.service;

import static com.google.common.base.Strings.isNullOrEmpty;

import com.ihunanren.biz.common.SpringBeanLoader;
import com.ihunanren.biz.function.domain.WeixinResp;
import com.ihunanren.biz.function.domain.WeixinRespProxy;
import com.ihunanren.dao.pojo.AppUserDO;

/**
 * @author kalman03
 */
public class WeixinServiceAdapter {
	

	private final String input;
	
	private final AppUserDO appUser;
	
	private final TextType type;
	
	public WeixinServiceAdapter(String input,AppUserDO appUser) {
		this.input = input;
		this.appUser = appUser;
		this.type = initType();
	}
	
	private TextType initType(){
		if(isNullOrEmpty(input))return TextType.UnKnow;
		String temp = input.trim().toLowerCase();
		for(TextType prefix : TextType.values()){
			for(String str : prefix.getName()){
				if (temp.indexOf(str) >= 0) {
					return prefix;
				}
			}
		}
		return TextType.UnKnow;
	}
	
	/***
	 * 是否注册了这个功能
	 * @return
	 */
	private boolean isSubscribe(){
		//FIXME
		return true;
	}
	
	@SuppressWarnings("unchecked")
	public String getResponse(){
		if(! isSubscribe()){
			return null;
		}
		if(type != null){
			IWeixinService<WeixinResp> weixinService = (IWeixinService<WeixinResp>) SpringBeanLoader.getSpringBean(type.getService());
			
			WeixinRespProxy proxy = new WeixinRespProxy(weixinService.getResponse(input));

			return proxy.toReply();
		}
		
		return null;
	}
	
	public TextType guessType(){
		return type;
	}
	
	enum TextType {
		UnKnow(new String[] {},""), 
		Ip(new String[] { "ip" },"ipService"), 
		IdentityCard(new String[] { "身份证" },"identityCardService"), 
		Weather(new String[] { "天气" },"weatherService"), 
		MobilePhone(new String[] { "电话","手机" },"mobilePhoneService"), 
		Time(new String[] { "时间","日期","几号","今天是","星期几" },"timeService"),
		Joke(new String[]{"笑话","幽默"},"jokeService"),
		SchoolCurriculum(new String[]{"课程表"},"schoolCurriculumService"),
		More(new String[]{"m"},"moreService");

		private String[] name;
		private String service;//对应spring xml的配置
		private TextType(String[] name, String service) {
			this.name = name;
			this.service = service;
		}
		public String[] getName() {
			return name;
		}
		public String getService() {
			return service;
		}
	}
}
